<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Short description for file
 *
 * PHP version 5.3.+
 *
 * @category   
 * @package    
 * @author     Marius Schmidt <marius.schmidt@gridground.de>
 * @copyright  2010-2012 Gridground UG (haftungsbeschränkt)
 * @license    http://www.gridground.de/license/brickscout.txt
 * @version    GIT: $Id$
 * @link       
 * @see        
 * @since      File available since 
 */

/**
 * Short description for class
 *
 * @category   
 * @package    
 * @author     Marius Schmidt <marius.schmidt@gridground.de>
 * @copyright  2010-2012 Gridground UG (haftungsbeschränkt)
 * @license    http://www.gridground.de/license/brickscout.txt
 * @version    Release: @package_version@
 * @link       
 * @see        
 * @since      Class available since Release 
 */
class Economic_WebService {

    //Service configuration
    public $config;
    // Soap client
    private $_client;
    // Debug access
    private $_accessGranted;
    private static $classmap = array(
        'ProductData' => 'ProductData',
        'ProductHandle' => 'Product',
    );
    
    private static $_webService = NULL;

    public static function getInstance() {
        if (NULL == self::$_webService) {
            $filePath = realpath(dirname(__FILE__) . '/economics.ini');
            if (!file_exists($filePath)) {
                throw new Exception('INI file could not be found');
            }
            $config = parse_ini_file($filePath);
            if (!(is_array($config) && count($config) > 0)) {
                throw new Exception('INI file corrupted, can not load settings');
            }

            self::$_webService = new Economic_WebService();
            self::$_webService->setConfig($config);
            self::$_webService->connect();
            var_dump(self::$_webService->isAccessGranted());
        }
        return self::$_webService;
    }

    public function setConfig(array $config) {
        $this->config = $config;
    }

    public function connect() {
        try {
            $this->initClient();
            $this->_client->Connect(array(
                'agreementNumber' => $this->config['agreement_number'],
                'userName' => $this->config['user_name'],
                'password' => $this->config['password']
            ));
            $this->_accessGranted = TRUE;
        } catch (AuthenticationException $ae) {
            throw new Exception("Falsch angemeldet");
        } catch (Exception $e) {
            $this->_accessGranted = FALSE;
            var_dump($e->getMessage());
            throw new Exception('Could not connect. Maybe the INI file contains invalid settings?');
        }
    }

    private function initClient() {
        $uri = $this->config['wsdl_endpoint'];
        $params = array(
            'trace' => TRUE,
            'exceptions' => TRUE,
            'classmap' => self::$classmap,
        );
        $this->_client = new SoapClient($uri, $params);
    }

    public function disconnect() {
        if (NULL != $this->_client) {
            $this->_client->Disconnect();
            $this->_client = NULL;
        } else {
            throw new Exception('Could not disconnect. No client initialized');
        }
    }

    /**
     * @return boolean
     */
    public function isAccessGranted() {
        return $this->_accessGranted;
    }

    public function soapCall($function_name, $argumentsArray) {
        return $this->_client->$function_name($argumentsArray);
    }

    public function getFunctions() {
        return $this->_client->__getFunctions();
    }

    public function getTypes() {
        return $this->_client->__getTypes();
    }


}
